package com.infitecs.eyas.nature;

import com.infitecs.eyas.autocomplete.service.CustomAutoCompleteServiceImpl;
import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.nature.dao.NatureDao;
import com.infitecs.eyas.nature.model.Help;
import com.infitecs.eyas.nature.model.Nature;
import com.infitecs.eyas.nature.service.NatureService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.quality.dao.AshDao;
import com.infitecs.eyas.quality.dao.BurstDao;
import com.infitecs.eyas.quality.dao.ElementDao;
import com.infitecs.eyas.quality.dao.IndustryDao;
import com.infitecs.eyas.quality.model.Burst;
import com.infitecs.eyas.step.service.StepService;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Demon on 2015/5/6.
 */
@Controller
@RequestMapping("/nature")
public class NatureController {
    @Autowired
    private NatureService natureService;

    @Autowired
    private StepService stepService;
    @Autowired
    private NatureDao natureDao;
    @Autowired
    private CustomAutoCompleteServiceImpl autoCompleteService;
    @Autowired
    private PowerDao powerDao;
    @Autowired
    private OperateFieldDao operateFieldDao;


    @RequestMapping(value = "getHelp/cityName",method = RequestMethod.POST)
    @ResponseBody
    public Help getHelp(@RequestParam("cityName")String cityName,HttpServletRequest request) {
        return natureService.getHelp(cityName);
    }

    @RequestMapping(value = "getNaAll",method = RequestMethod.GET)
    @ResponseBody
    public List<Help>getNaAll(HttpSession request){
        return natureService.getAllDictNa();
    }


    @RequestMapping(value = "save", method = RequestMethod.GET)
    @ResponseBody
    public void save(HttpSession session) {
        stepService.updateStepStateFinish(Integer.parseInt(session.getAttribute("powerId").toString()), Constant.STEP_ID_5);
    }

    @RequestMapping(value = "", method = {RequestMethod.GET, RequestMethod.POST})
    public String getNaturePage(HttpSession session, HttpServletRequest request) {
        autoCompleteService.initAutoComplete();
        String cId = (String) session.getAttribute("caseId");
        if (cId == null) {
            return "nature/nature";
        }
        int caseId = Integer.parseInt(cId);
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        Nature nature = natureDao.getNatureByPowerCaseId(caseId);
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("nature", nature);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "nature/nature";
    }

    @RequestMapping(value = "/calculateLocalPressureAndOther",method = {RequestMethod.POST})
    @ResponseBody
    public HashMap calculateLocalPressureAndOther(HttpSession httpSession) {
        int powerId = Integer.parseInt(httpSession.getAttribute("powerId").toString());
        stepService.updateStepStateing(powerId, Constant.STEP_ID_5);
        return natureService.calculateLocalPressureByFormula21(powerId);
    }

    @RequestMapping(value = "/computerAirMoisture",method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResponse computerAirMoisture(HttpSession httpSession) {
        int powerId = Integer.parseInt(httpSession.getAttribute("powerId").toString());
        String rt= String.valueOf(natureService.calculateAnnualAverageAirMoistureByFormula23(powerId));
        stepService.updateStepStateing(powerId, Constant.STEP_ID_5);
        return new AjaxResponse(rt);
    }

    @RequestMapping(value = "/computerLocalDensityMoistAir",method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResponse computerLocalDensityMoistAir(HttpSession httpSession) {
        int powerId = Integer.parseInt(httpSession.getAttribute("powerId").toString());
        String rt= String.valueOf(natureService.calculateLocalDensityMoistAirByFormula24(powerId));
        stepService.updateStepStateing(powerId, Constant.STEP_ID_5);
        return new AjaxResponse(rt);
    }

    @RequestMapping(value = "/updateAltitude",method = {RequestMethod.POST})
    @ResponseBody
    public void updateAltitude(@RequestParam("altitude")double altitude, HttpSession session) {
        int powerId = Integer.parseInt(session.getAttribute("powerId").toString());
        natureService.updateAltitude(altitude,powerId);
        stepService.updateStepStateing(powerId, Constant.STEP_ID_5);
    }

    @RequestMapping(value = "/updateAnnualAverageTemperature",method = {RequestMethod.POST})
    @ResponseBody
    public void updateAnnualAverageTemperature(@RequestParam("annualAverageTemperature")double annualAverageTemperature, HttpSession session) {
        int powerId = Integer.parseInt(session.getAttribute("powerId").toString());
        natureService.updateAnnualAverageTemperature(annualAverageTemperature,powerId);
        stepService.updateStepStateing(powerId, Constant.STEP_ID_5);
    }

    @RequestMapping(value = "/updateLocalPressure",method = {RequestMethod.POST})
    @ResponseBody
    public void updateLocalPressure(@RequestParam("localPressure")double localPressure, HttpSession session) {
        int powerId = Integer.parseInt(session.getAttribute("powerId").toString());
        natureService.updateLocalPressure(localPressure,powerId);
        stepService.updateStepStateing(powerId, Constant.STEP_ID_5);
    }

    @RequestMapping(value = "/updateLocalPressureOther",method = {RequestMethod.POST})
    @ResponseBody
    public void updateLocalPressureOther(@RequestParam("localPressureOther")double localPressureOther, HttpSession session) {
        int powerId = Integer.parseInt(session.getAttribute("powerId").toString());
        natureService.updateLocalPressureOther(localPressureOther,powerId);
        stepService.updateStepStateing(powerId, Constant.STEP_ID_5);
    }

    @RequestMapping(value = "/updateOutdoorCalculatedTemperature",method = {RequestMethod.POST})
    @ResponseBody
    public void updateOutdoorCalculatedTemperature(@RequestParam("outdoorCalculatedTemperature")double outdoorCalculatedTemperature, HttpSession session) {
        int powerId = Integer.parseInt(session.getAttribute("powerId").toString());
        natureService.updateOutdoorCalculatedTemperature(outdoorCalculatedTemperature,powerId);
        stepService.updateStepStateing(powerId, Constant.STEP_ID_5);
    }

    @RequestMapping(value = "/updateSummerOutdoorCalculateTemperature",method = {RequestMethod.POST})
    @ResponseBody
    public void updateSummerOutdoorCalculateTemperature(@RequestParam("summerOutdoorCalculateTemperature")double summerOutdoorCalculateTemperature, HttpSession session) {
        int powerId = Integer.parseInt(session.getAttribute("powerId").toString());
        natureService.updateSummerOutdoorCalculateTemperature(summerOutdoorCalculateTemperature,powerId);
//        int caseId = Integer.parseInt(session.getAttribute("caseId").toString());
//        stepService.updateStepStateing(caseId, Constant.STEP_ID_5);
    }

    @RequestMapping(value = "/updateAnnualAverageRelativeHumidity",method = {RequestMethod.POST})
    @ResponseBody
    public void updateAnnualAverageRelativeHumidity(@RequestParam("annualAverageRelativeHumidity")double annualAverageRelativeHumidity, HttpSession session) {
        int powerId = Integer.parseInt(session.getAttribute("powerId").toString());
        natureService.updateAnnualAverageRelativeHumidity(annualAverageRelativeHumidity,powerId);
        stepService.updateStepStateing(powerId, Constant.STEP_ID_5);
    }

    @RequestMapping(value = "/updateMinDailyAverageTemperature",method = {RequestMethod.POST})
    @ResponseBody
    public void updateMinDailyAverageTemperature(@RequestParam("minDailyAverageTemperature")double minDailyAverageTemperature, HttpSession session) {
        int powerId = Integer.parseInt(session.getAttribute("powerId").toString());
        natureService.updateMinDailyAverageTemperature(minDailyAverageTemperature,powerId);
        stepService.updateStepStateing(powerId, Constant.STEP_ID_5);
    }

    @RequestMapping(value = "/updateAnnualAverageAirMoisture",method = {RequestMethod.POST})
    @ResponseBody
    public void updateAnnualAverageAirMoisture(@RequestParam("annualAverageAirMoisture")double annualAverageAirMoisture, HttpSession session) {
        int powerId = Integer.parseInt(session.getAttribute("powerId").toString());
        natureService.updateAnnualAverageAirMoisture(annualAverageAirMoisture,powerId);
        stepService.updateStepStateing(powerId, Constant.STEP_ID_5);
    }

    @RequestMapping(value = "/updateLocalDensityMoistAir",method = {RequestMethod.POST})
    @ResponseBody
    public void updateLocalDensityMoistAir(@RequestParam("localDensityMoistAir")double localDensityMoistAir, HttpSession session) {
        int powerId = Integer.parseInt(session.getAttribute("powerId").toString());
        natureService.updateLocalDensityMoistAir(localDensityMoistAir,powerId);
        stepService.updateStepStateing(powerId, Constant.STEP_ID_5);
    }

    @RequestMapping(value = "/getNature",method = {RequestMethod.GET})
    @ResponseBody
    public Nature getNatureByPowerId(HttpSession session) {
        int powerId = Integer.parseInt(session.getAttribute("powerId").toString());
        stepService.updateStepStateing(powerId, Constant.STEP_ID_5);
        return natureService.getNatureByPowerId(powerId);
    }

    @RequestMapping(value = "natureSave", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse natureSave(Nature nature) {
        try {
            natureService.natureSave(nature);
            return new AjaxResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
        }
    }

}
